package com.gxk.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxk.common.utils.PageUtils;
import com.gxk.common.utils.Query;

import com.gxk.gulimall.product.dao.CategoryDao;
import com.gxk.gulimall.product.entity.CategoryEntity;
import com.gxk.gulimall.product.service.CategoryService;

import javax.annotation.Resource;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Resource
    CategoryDao categoryDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        //1.查出所有分类
        List<CategoryEntity> entities = categoryDao.selectList(null);
        CategoryEntity root = new CategoryEntity();
        //2.组装成树形结构
        List<CategoryEntity> entities1 = entities.stream().filter(categoryEntity ->
                categoryEntity.getParentCid().toString().equals("0")
        ).map((menu)->{
            menu.setChildren(getChildren(menu,entities));
            return menu;
        }).sorted((menu1,menu2)->{
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return entities1;
    }

    @Override
    public void removeByMenuIds(List<Long> list) {
        //TODO 1.删除之前检查是否被别的菜单引用
        baseMapper.deleteBatchIds(list);
    }

    @Override
    public Long[] fingCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> ParentPaths = findfatherPath(catelogId,paths);
        Collections.reverse(ParentPaths);
        return ParentPaths.toArray(new Long[ParentPaths.size()]);
    }
    private List<Long> findfatherPath(Long catelogId,List<Long> paths) {
        paths.add(catelogId);
        CategoryEntity byId = this.getById(catelogId);
        if (byId.getParentCid() != 0){
            findfatherPath(byId.getParentCid(),paths);
        }
        return paths;
    }

    //递归查找所有菜单的子菜单
    private List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> all) {
        List<CategoryEntity> children = all.stream().filter(categoryEntity ->
            categoryEntity.getParentCid().equals(root.getCatId())

        ).map(categoryEntity -> {
            categoryEntity.setChildren(getChildren(categoryEntity,all));
            return categoryEntity;
        }).sorted((menu1,menu2)->{
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());

        return children;
    }

}